/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoginFrame.java
 *
 * Created on 17/07/2013, 06:40:50 AM
 */
package pe.edu.unmsm.cinfo.siga.login;

import javax.swing.JOptionPane;

/**
 *
 * @author lartkma
 */
public class LoginFrame extends javax.swing.JFrame {

    /** Creates new form LoginFrame */
    public LoginFrame(OKPasswordListener passwordListener, int claseUsuario) {
        this.passwordListener = passwordListener;
        this.claseUsuario = claseUsuario;
        initComponents();
        centrar();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel2 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel3 = new javax.swing.JLabel();
        usuarioTextField = new javax.swing.JTextField();
        SalirButton = new javax.swing.JButton();
        ingresarButton = new javax.swing.JButton();
        contraseniaTextField = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Bienvenidos al Sistema de Gestion Academica - CINFO");
        setResizable(false);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pe/edu/unmsm/cinfo/siga/login/logo.png"))); // NOI18N

        jLabel2.setText("Usuario:");

        jLabel3.setText("Contraseña:");

        usuarioTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ingresarButtonActionPerformed(evt);
            }
        });

        SalirButton.setText("Salir");
        SalirButton.setMargin(new java.awt.Insets(5, 10, 5, 10));
        SalirButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SalirButtonActionPerformed(evt);
            }
        });

        ingresarButton.setText("Ingresar");
        ingresarButton.setMargin(new java.awt.Insets(5, 10, 5, 10));
        ingresarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ingresarButtonActionPerformed(evt);
            }
        });

        contraseniaTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ingresarButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(ingresarButton, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(SalirButton, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(usuarioTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE)
                            .addComponent(contraseniaTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE)))
                    .addComponent(jLabel1))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(usuarioTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(contraseniaTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SalirButton)
                    .addComponent(ingresarButton, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void SalirButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SalirButtonActionPerformed
    dispose();
}//GEN-LAST:event_SalirButtonActionPerformed

private void ingresarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ingresarButtonActionPerformed
    try {
        if (!esNombreValido()) {
            JOptionPane.showMessageDialog(this, "Escriba un nombre válido", "SIGA - CINFO", JOptionPane.INFORMATION_MESSAGE);
        } else if (contraseniaValida()) {
            DatosUsuario datos = new DatosUsuario();
            datos.setNombre(usuarioTextField.getText());
            dispose();
            passwordListener.OKPassword(datos);
        } else {
            JOptionPane.showMessageDialog(this, "Contraseña incorrecta", "SIGA - CINFO", JOptionPane.ERROR_MESSAGE);
        }
    } catch (SIGAException ex) {
        JOptionPane.showMessageDialog(this, ex.getMessage(), "SIGA - CINFO", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_ingresarButtonActionPerformed

    private boolean contraseniaValida() {
        boolean consulta;
        if (claseUsuario == ClaseUsuario.CUALQUIERA) {
            consulta = VerficarContrasenia.get().verificar(usuarioTextField.getText(), contraseniaTextField.getText());
        } else {
            consulta = VerficarContrasenia.get().verificar(usuarioTextField.getText(), contraseniaTextField.getText(), claseUsuario);
        }
        return consulta;
    }

    private boolean esNombreValido() {
        return usuarioTextField.getText().trim().length() != 0;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton SalirButton;
    private javax.swing.JPasswordField contraseniaTextField;
    private javax.swing.JButton ingresarButton;
    private javax.swing.JTextField usuarioTextField;
    // End of variables declaration//GEN-END:variables
    private OKPasswordListener passwordListener;
    private int claseUsuario;

    @Override
    protected void finalize() throws Throwable {
        VerficarContrasenia.end();
        super.finalize();
    }

    private void centrar() {
        setLocationRelativeTo(null);
    }
}
